package com.bjim.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.bjim.AResult;
import com.bjim.model.Dep;

import cn.dreampie.route.annotation.API;
import cn.dreampie.route.annotation.GET;
import cn.dreampie.route.annotation.POST;

/**
 * 部门API类
 * @author chen
 *
 */
@API("dep")
public class DepResource extends ApiResource{
	
	/**
	 * 获取所有机构和联系人
	 * @return
	 */
	@GET("/all")
	@POST("/all")
	public AResult getAll(){
		Map<Object,Map> depTree=new HashMap<>();
		Map<Object,List<Dep>> key_dep=new LinkedHashMap<Object,List<Dep>>();
		List<Dep> ls=Dep.dao.queryAlldep();
		for(Dep r:ls){
			r.put("users", r.findEmps());
			Object parentdepid=r.get("parentdepid");
			if(key_dep.containsKey(parentdepid)){
				key_dep.get(parentdepid).add(r);
			}else{
				List<Dep> deps=new ArrayList<Dep>();
				deps.add(r);
				key_dep.put(parentdepid, deps);
			}
		}
		for(Dep r:ls){
			r.put("deps", key_dep.get(r.get("depid"))==null?new ArrayList<Dep>():key_dep.get(r.get("depid")));
		}
		return new AResult(key_dep.get(null));
	}
	/**
	 * 获取部门信息
	 * @param depid
	 * @return
	 */
	@GET("/:depid")
	@POST("/:depid")
	public AResult getDepInfo(String depid){
		return new AResult(Dep.dao.findById(depid));
	}
	
	/**
	 * 获取下级部门列表
	 * @param depid
	 * @return
	 */
	@GET("/:depid/deps")
	@POST("/:depid/deps")
	public AResult getDeps(String depid){
		return new AResult(new Dep().set("depid", depid).finddeps());
	}
	
	/**
	 * 获取部门人员列表
	 * @param depid
	 * @return
	 */
	@GET("/:depid/emps")
	@POST("/:depid/emps")
	public AResult getEmps(String depid){
		return new AResult(new Dep().set("depid", depid).findEmps());
	}
	
	/**
	 * 根据部门名称查找部门
	 * @param depname
	 * @return
	 */
	@GET("/find/:depname")
	public AResult findBydepname(String  depname){
		return new AResult(Dep.dao.findBydepname(depname));
	}
	
	public static void main(String[] args) {
		Map test=new HashMap();
		test.put(null, "");
	}
}
